/**
 * 
 */
package ar.edu.unq.servlets;

import java.io.IOException;
import java.text.DecimalFormat;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author nicolas
 *
 */
public abstract class Calc extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private double result=0;

    public double getResult() {
		return result;
	}

	public void setResult(double result) {
		this.result = result;
	}

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        this.processRequest(request, response);
    }
    
    private void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException 
    {

    	this.setResult(this.performOperation(Double.parseDouble(request.getParameter("value1")), Double.parseDouble(request.getParameter("value2"))));
		response.sendRedirect("./calc.jsp?result=" + this.getResult());
	}

	/** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    @Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        this.processRequest(request, response);
    }	
    
    protected abstract double performOperation(double value1, double value2);
	
}
